import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
import Notify from '@vant/weapp/notify/notify';
import { request } from '../../assets/js/request.js';
Page({
  onSearch: function (e) {
    wx.showLoading({
      title: '正在加载',
      mask: true,
    })
    request('/keywordSelect.php', { keyword: e.detail })
      .then((res)=>{
        this.setData({
          classItems: res.data
        })
      })
      .catch((err)=>{
        Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
      })
  },

  onCancel:function(e){
    this.setData({
      classItems: []
    })
  },

  handlerApply:function(e){
    Dialog.confirm({
      title: '提示',
      message: '确定加入该课程吗？'
    }).then(() => {
      wx.showLoading({
        title: '请稍后',
        mask: true,
      })
      let reqData = {
        uid: this.data.uid,
        classId: e.currentTarget.id
      }
      request('/applyClass.php', reqData)
        .then((res)=>{
          if (res.data == 'ok') {
            Toast({
              type: 'success',
              mask: true,
              message: '申请成功',
              duration: 1000
            });
          }else if(res.data == 'exist'){
            Toast({
              type: 'fail',
              mask: true,
              message: '请勿重复添加',
              duration: 1000
            });
          }else if(res.data == 'fail'){
            Toast({
              type: 'fail',
              mask: true,
              message: '申请失败',
              duration: 1000
            });
          }
        })
        .catch((err)=>{
          Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
        })
    }).catch(() => {
      
    });
  },

  data: {
    classItems:[],
    uid: '',
    status:''
  },

  onLoad: function (options) {

  },

  onShow: function () {
    this.setData({
      uid: wx.getStorageSync('uid'),
      status: wx.getStorageSync('status')
    })
  },

  onHide: function () {
    this.setData({
      classItems: []
    })
  },

  onUnload: function () {

  },

  onPullDownRefresh: function () {

  },

  onReachBottom: function () {
    
  },

})